package cn.iocoder.yudao.module.trade.delivery.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.infrastructure.user.AdminUserApi;
import cn.iocoder.yudao.module.trade.delivery.dal.dataobject.DeliveryPickUpStoreDO;
import cn.iocoder.yudao.module.trade.delivery.dal.mysql.DeliveryPickUpStoreMapper;
import cn.iocoder.yudao.module.trade.delivery.vo.DeliveryPickUpBindReqVO;
import cn.iocoder.yudao.module.trade.delivery.vo.DeliveryPickUpStorePageReqVO;
import cn.iocoder.yudao.module.trade.delivery.vo.DeliveryPickUpStoreSaveReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserLastShopId;
import static cn.iocoder.yudao.module.api.trade.ErrorCodeConstants.PICK_UP_STORE_NOT_EXISTS;

/**
 * 自提门店 Service 实现类
 *
 * @author jason
 */
@Service
@Validated
public class DeliveryPickUpStoreServiceImpl implements DeliveryPickUpStoreService {

    @Resource
    private DeliveryPickUpStoreMapper deliveryPickUpStoreMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long saveDeliveryPickUpStore(DeliveryPickUpStoreSaveReqVO reqVO) {
        if (reqVO.getShopId() == null || reqVO.getShopId() <= 0) {
            reqVO.setShopId(getLoginUserLastShopId());
        }
        if (reqVO.getId() != null && reqVO.getId() > 0) {
            // 校验存在
            DeliveryPickUpStoreDO pickUpStoreDO = validateDeliveryPickUpStoreExists(reqVO.getId());
            List<Long> verifyUserIds = pickUpStoreDO.getVerifyUserIds();
            if (verifyUserIds == null) {
                verifyUserIds = new ArrayList<>();
            }
            if (!CollUtil.contains(verifyUserIds, getLoginUserId())) {
                verifyUserIds.add(getLoginUserId());
            }
            // 更新
            DeliveryPickUpStoreDO updateObj = BeanUtils.toBean(reqVO, DeliveryPickUpStoreDO.class);
            updateObj.setVerifyUserIds(verifyUserIds);
            deliveryPickUpStoreMapper.updateById(updateObj);
            // 返回
            return pickUpStoreDO.getId();
        }
        // 插入
        DeliveryPickUpStoreDO pickUpStoreDO = BeanUtils.toBean(reqVO, DeliveryPickUpStoreDO.class);
        pickUpStoreDO.setVerifyUserIds(CollUtil.newArrayList(getLoginUserId()));
        deliveryPickUpStoreMapper.insert(pickUpStoreDO);
        // 返回
        return pickUpStoreDO.getId();
    }

    @Override
    public void deleteDeliveryPickUpStore(Long id) {
        // 校验存在
        validateDeliveryPickUpStoreExists(id);
        // 删除
        deliveryPickUpStoreMapper.deleteById(id);
    }

    private DeliveryPickUpStoreDO validateDeliveryPickUpStoreExists(Long id) {
        DeliveryPickUpStoreDO deliveryPickUpStore = deliveryPickUpStoreMapper.selectById(id);
        if (deliveryPickUpStore == null) {
            throw exception(PICK_UP_STORE_NOT_EXISTS);
        }
        return deliveryPickUpStore;
    }

    @Override
    public DeliveryPickUpStoreDO getDeliveryPickUpStore(Long id) {
        return deliveryPickUpStoreMapper.selectById(id);
    }

    @Override
    public List<DeliveryPickUpStoreDO> getDeliveryPickUpStoreList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return deliveryPickUpStoreMapper.selectByIds(ids);
    }

    @Override
    public DeliveryPickUpStoreDO getDeliveryPickUpStoreByShopName(Long shopId, String name) {
        return deliveryPickUpStoreMapper.selectByByShopName(shopId, name);
    }

    @Override
    public List<DeliveryPickUpStoreDO> getDeliveryPickUpStoreListByStatus(Integer status) {
        return deliveryPickUpStoreMapper.selectListByStatus(status);
    }

    @Override
    public PageResult<DeliveryPickUpStoreDO> getDeliveryPickUpStorePage(DeliveryPickUpStorePageReqVO pageReqVO) {
        return deliveryPickUpStoreMapper.selectPage(pageReqVO);
    }

    @Override
    public void bindDeliveryPickUpStore(DeliveryPickUpBindReqVO bindReqVO) {
        // 1.1 校验门店存在
        validateDeliveryPickUpStoreExists(bindReqVO.getId());
        // 1.2 校验用户存在
        adminUserApi.validateUserList(bindReqVO.getVerifyUserIds());

        // 2. 更新
        DeliveryPickUpStoreDO updateObj = BeanUtils.toBean(bindReqVO, DeliveryPickUpStoreDO.class);
        deliveryPickUpStoreMapper.updateById(updateObj);
    }

}
